<?php

namespace app\pc\controller;


use basic\WapBasic;
use service\AlipayTradeWapService;
use service\UtilService;
use app\wap\model\activity\EventSignUp;
use service\JsonService;
use service\SystemConfigService;
use service\WxpayService;
use think\Url;
use think\Db;

use service\WechatTemplateService;
use app\wap\model\user\User;
use app\wap\model\store\StoreOrder;
use app\wap\model\store\StoreOrderStatus;
use app\wap\model\user\WechatUser;
use app\wap\model\user\MemberShip;
use app\admin\model\wechat\WechatMessage;
use app\pc\model\tindex\Openvip;//会员卡
/**
 * 支付宝支付
 * Class Alipay
 * @package app\wap\controller
 */
class Pay extends AuthController
{
    protected function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $this->isopen();
            $wechat = SystemConfigService::more(['wechat_appid','wechat_appsecret','wechat_token']);
            $payment = SystemConfigService::more(['pay_weixin_mchid','pay_weixin_client_cert','pay_weixin_client_key','pay_weixin_key','pay_weixin_open']);
           
          $this->appid=isset($wechat['wechat_appid']) ? $wechat['wechat_appid']:'';
             $this->secret=isset($wechat['wechat_appsecret']) ? $wechat['wechat_appsecret']:'';
             $this->token=isset($wechat['wechat_token']) ? $wechat['wechat_token']:'';
             $this->mchid=$payment['pay_weixin_mchid'];
             $this->apiKey=$payment['pay_weixin_key'];



    

        $this->assign('overallShareWechat', json_encode([]));
        $this->assign('userInfo', ['uid' => 0]);
    }

    /**
     * 支付页微信端提示切换浏览器,其他浏览器直接支付
     * @param string $info 加密后的支付参数
     * @return mixed|void
     */
    public function index($payType)
    {

     
    }

    public function create_order($special_id,$payType,$cateid)
    {
        if($this->isopenc){
             Header("Location:".$_SERVER['HTTP_REFERER']);
        }
     
        $id=$special_id;
   
        if(!$id) return JsonService::fail('参数错误!');
        $order = StoreOrder::cacheMemberCreateOrder($this->userInfo['uid'],$id,$payType);

        Openvip::add(array("userid"=>$this->uid,"cateid"=>$cateid,'orderid'=> $order['order_id'],'addtime'=>time(),"status"=>1));
        $orderId = $order['order_id'];
        $info = compact('orderId');
        if ($orderId) {
            $orderInfo = StoreOrder::where('order_id', $orderId)->find();
            if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
            if ($orderInfo['paid']) exception('支付已支付!');
            if (bcsub((float)$orderInfo['pay_price'], 0, 2) <= 0) {
                if (StoreOrder::jsPayMePrice($orderId, $this->userInfo['uid']))
                    return JsonService::status('success', '领取成功', $info);
                else
                    return JsonService::status('pay_error', StoreOrder::getErrorInfo());
            }
        } else {
            return JsonService::fail(StoreOrder::getErrorInfo('领取失败!'));
        }

        if($payType=="weixin"){

            $wxPay = new WxpayService($this->mchid,$this->appid,$this->apiKey);//实例化微信扫码类
            $outTradeNo = $orderInfo['id'];     //你自己的商品订单号
            $notifyUrl = "https://".$_SERVER['HTTP_HOST'].Url::build('Paysuccess/wapay_success_notify',array("orderId"=>$orderId,'cateid'=>$cateid));   
       
            //付款成功后的回调地址(不要有问号)
            $payTime = time();      //付款时间
            $orderName="购买会员";
            $payAmount=$orderInfo['pay_price'];
            $wechat_result = $wxPay->createJsBizPackage($payAmount,$outTradeNo,$orderName,$notifyUrl,$payTime);//创建成功会微信返回的数据
            $this->assign('wechat_result',$wechat_result);
        }
        if($payType=="alipay"){
            $info['pay_price'] = $orderInfo['pay_price'];
            $info['orderName'] = '会员购买';
            $alipayurl=Url::build('alipay/index',array('info', base64_encode(json_encode($info))));

            header("Location: " .$alipayurl);
        }

        $this->assign('orderInfo',$orderInfo);
        $this->assign('payType',$payType);
        return $this->fetch("index");
    }
    public function getorderLog(){
      
        if($this->isopen){
            $data=array("url"=>Url::build('tindex/start'));
            return JsonService::successful("ok",$data);
        }
        JsonService::fail('fail');
    }


}